﻿using System;
using System.Web.UI;
using JQuery.WebKit.Base;
using JQuery.WebKit.WebCommon;
using JQuery.WebKit.WebCommon.Attributes;

namespace JQuery.WebKit
{
    public class JDatePicker : JControl
    {
        internal override PostType PostType { get { return PostType.Value; } }
        public static string JQueryDateFormat = "mm/dd/yy";

        public string TooltipText { get; set; }

        #region options
        [JavaScript("disabled", Category = "options")]
        public bool? Disabled { get; set; }

        [JavaScript("altField", Category = "options")]
        public string AltField { get; set; }

        [JavaScript("altFormat", Category = "options")]
        public string AltFormat { get; set; }

        [JavaScript("appendText", Category = "options")]
        public string AppendText { get; set; }

        [JavaScript("autoSize", Category = "options")]
        public bool? AutoSize { get; set; }

        [JavaScript("buttonImage", Category = "options")]
        public string ButtonImage { get; set; }
        
        [JavaScript("buttonImageOnly", Category = "options")]
        public bool? ButtonImageOnly { get; set; }
        
        [JavaScript("buttonText", Category = "options")]
        public string ButtonText { get; set; }
        
        [JavaScript("calculateWeek", Category = "options")]
        public string CalculateWeek { get; set; }
        
        [JavaScript("changeMonth", Category = "options")]
        public bool? ChangeMonth { get; set; }
        
        [JavaScript("changeYear", Category = "options")]
        public bool? ChangeYear { get; set; }
        
        [JavaScript("closeText", Category = "options")]
        public string CloseText { get; set; }
        
        [JavaScript("constrainInput", Category = "options")]
        public bool? ConstrainInput { get; set; }
        
        [JavaScript("currentText", Category = "options")]
        public string CurrentText { get; set; }
        
        [JavaScript("dateFormat", Category = "options")]
        public string DateFormat { get; set; }
        
        [JavaScript("dayNames", Category = "options")]
        public string[] DayNames { get; set; }
        
        [JavaScript("dayNamesMin", Category = "options")]
        public string[] DayNamesMin { get; set; }
        
        [JavaScript("dayNamesShort", Category = "options")]
        public string[] DayNamesShort { get; set; }
        
        [JavaScript("defaultDate", Category = "options")]
        public string DefaultDate { get; set; }
        
        [JavaScript("duration", Category = "options")]
        public string Duration { get; set; }
        
        [JavaScript("firstDay", Category = "options")]
        public int FirstDay { get; set; }
        
        [JavaScript("gotoCurrent", Category = "options")]
        public bool? GotoCurrent { get; set; }
        
        [JavaScript("hideIfNoPrevNext", Category = "options")]
        public bool? HideIfNoPrevNext { get; set; }
        
        [JavaScript("isRTL", Category = "options")]
        public bool? IsRTL { get; set; }
        
        [JavaScript("maxDate", Category = "options")]
        public string MaxDate { get; set; }
        
        [JavaScript("minDate", Category = "options")]
        public string MinDate { get; set; }
        
        [JavaScript("monthNames", Category = "options")]
        public string[] MonthNames { get; set; }
        
        [JavaScript("monthNamesShort", Category = "options")]
        public string[] MonthNamesShort { get; set; }
        
        [JavaScript("navigationAsDateFormat", Category = "options")]
        public bool? NavigationAsDateFormat { get; set; }
        
        [JavaScript("nextText", Category = "options")]
        public string NextText { get; set; }
        
        [JavaScript("numberOfMonths", Category = "options")]
        public int[] NumberOfMonths { get; set; }
        
        [JavaScript("prevText", Category = "options")]
        public string PrevText { get; set; }
        
        [JavaScript("selectOtherMonths", Category = "options")]
        public bool? SelectOtherMonths { get; set; }
        
        [JavaScript("shortYearCutoff", Category = "options")]
        public int? ShortYearCutoff { get; set; }
        
        [JavaScript("showAnim", Category = "options")]
        public string ShowAnim { get; set; }
        
        [JavaScript("showButtonPanel", Category = "options")]
        public bool? ShowButtonPanel { get; set; }
        
        [JavaScript("showCurrentAtPos", Category = "options")]
        public int? ShowCurrentAtPos { get; set; }
        
        [JavaScript("showMonthAfterYear", Category = "options")]
        public bool? ShowMonthAfterYear { get; set; }
        
        [JavaScript("showOn", Category = "options")]
        public string ShowOn { get; set; }
        
        [JavaScript("showOtherMonths", Category = "options")]
        public bool? ShowOtherMonths { get; set; }
        
        [JavaScript("showWeek", Category = "options")]
        public bool? ShowWeek { get; set; }
        
        [JavaScript("stepMonths", Category = "options")]
        public int? StepMonths { get; set; }
        
        [JavaScript("weekHeader", Category = "options")]
        public string WeekHeader { get; set; }
        
        [JavaScript("yearRange", Category = "options")]
        public string YearRange { get; set; }
        
        [JavaScript("yearSuffix", Category = "options")]
        public string YearSuffix { get; set; }
        #endregion

        public override object Value
        {
            get
            {
                return base.Value;
            }
            set
            {
                base.Value = value;
                EnsureDate();
            }
        }

        public JDatePicker() : base("input") { }
        public JDatePicker(string tag) : base("input") { }

        protected override void Render(HtmlTextWriter writer)
        {
            this.Attributes.Add("type", "text");
            this.Attributes.Add("name", this.ClientID);
            this.Attributes.Add("style", "width: 100%");
            if (!String.IsNullOrEmpty(TooltipText))
                this.Attributes.Add("title", TooltipText);
            base.Render(writer);
        }

        private void SetDefaults()
        {
            if (string.IsNullOrEmpty(DateFormat))
            {
                DateFormat = JQueryDateFormat;
            }
            if (base.Required.HasValue && base.Required.Value)
            {
                this.AddValidation(ValidationFlags.Mandatory);
                base.Value = DateTime.Now.Date;
                DefaultDate = DateTime.Now.Date.ToString(JQueryDateFormat);
            }
        }

        private void EnsureDate()
        {
            SetDate(base.Value);
        }

        private void SetDate(object value)
        {
            if (value == null)
            {
                if (base.Required.HasValue && base.Required.Value)
                {
                    base.Value = DateTime.Now.Date;
                    DefaultDate = DateTime.Now.Date.ToString(JQueryDateFormat);
                }
                return;
            }
            DateTime dt;
            if (DateTime.TryParse(value.ToString(), out dt))
            {
                base.Value = dt.Date;
                DefaultDate = dt.ToString(JQueryDateFormat);
            }
            else if (base.Required.HasValue && base.Required.Value)
            {
                base.Value = DateTime.Now.Date;
                DefaultDate = DateTime.Now.Date.ToString(JQueryDateFormat);
            }
            else
            {
                base.Value = null;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            SetDefaults();
            EnsureDate();
            string script = string.Format("$('#{0}').datepicker({1});", this.ClientID, this.GetJSProperties("options"));
            this.Page.Resources.Add(new WebScriptResource(typeof(JDatePicker), this.ClientID, ResourceType.Javascript, script, LoadEvent.DocumentReady));

            base.OnPreRender(e);
        }
    }
}
